package com.lxmes.jiesuan.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.lxmes.business.domain.CcRukuMain;
import com.lxmes.business.mapper.CcRukuMainMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.jiesuan.mapper.JiesuanZhikuanMapper;
import com.lxmes.jiesuan.domain.JiesuanZhikuan;
import com.lxmes.jiesuan.service.IJiesuanZhikuanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 支款记账Service业务层处理
 * 
 * @author zhangzhiyu
 * @date 2022-09-26
 */
@Service
public class JiesuanZhikuanServiceImpl implements IJiesuanZhikuanService 
{
    @Autowired
    private JiesuanZhikuanMapper jiesuanZhikuanMapper;
    @Autowired
    private CcRukuMainMapper ccRukuMainMapper;

    /**
     * 查询支款记账
     * 
     * @param id 支款记账主键
     * @return 支款记账
     */
    @Override
    public JiesuanZhikuan selectJiesuanZhikuanById(Long id)
    {
        return jiesuanZhikuanMapper.selectJiesuanZhikuanById(id);
    }

    /**
     * 查询支款记账列表
     * 
     * @param jiesuanZhikuan 支款记账
     * @return 支款记账
     */
    @Override
    public List<JiesuanZhikuan> selectJiesuanZhikuanList(JiesuanZhikuan jiesuanZhikuan)
    {
        return jiesuanZhikuanMapper.selectJiesuanZhikuanList(jiesuanZhikuan);
    }

    /**
     * 新增支款记账
     * 
     * @param jiesuanZhikuan 支款记账
     * @return 结果
     */
    @Override
    @Transactional
    public int insertJiesuanZhikuan(JiesuanZhikuan jiesuanZhikuan)
    {
            BigDecimal meoney=jiesuanZhikuan.getZkMoney().add(jiesuanZhikuan.getYhMoney());
            BigDecimal zk= jiesuanZhikuan.getZkMoney();
            BigDecimal yh= jiesuanZhikuan.getYhMoney();
            BigDecimal yf= jiesuanZhikuan.getYfMoney();
            if(meoney.compareTo(yf)==1){
                throw new ServiceException("支款金额加优惠金额大于应付金额！");
            }
            if(yh.compareTo(yf)==1){
                throw new ServiceException("优惠金额大于应付金额！");
            }
            if(yh.compareTo(zk)==1){
                throw new ServiceException("优惠金额大于支款金额！");
            }
            if(zk.compareTo(yf)==1){
                throw new ServiceException("支款金额大于应付金额！");
            }
            if(zk.compareTo(new BigDecimal("0"))==0){
                if(!"赊销".equals(jiesuanZhikuan.getFkfs())){
                    throw new ServiceException("支款金额不能为0.00  ---支款方式（赊销）除外");
                }
            }
/*
            if("原材料入库".equals(jiesuanZhikuan.getYwType()) ){
                if( StringUtils.isNull(jiesuanZhikuan.getYwId())){
                    throw new ServiceException("请选择业务单号不能为空！");
                }
                CcRukuMain ruku = ccRukuMainMapper.selectCcRukuMainByDanHao(jiesuanZhikuan.getYwId());
                if( ruku==null){
                    throw new ServiceException("未找到该业务单！"+jiesuanZhikuan.getYwId());
                }
                BigDecimal totalPrice=ruku.getTotalPrice();
                Map<String, String> map = jiesuanZhikuanMapper.selectTjZhikuanByYwId(jiesuanZhikuan.getYwId());
                if(StringUtils.isNotEmpty(map)){
                    BigDecimal zkmoney=new BigDecimal( String.valueOf(map.get("zkmoney")));
                    BigDecimal yhmoney=new BigDecimal( String.valueOf(map.get("yhmoney")));
                    BigDecimal zMoney= zkmoney.add(yhmoney).add(jiesuanZhikuan.getZkMoney()).add(jiesuanZhikuan.getYhMoney());
                    if(zMoney.compareTo(totalPrice)==1){
                        throw new ServiceException("支款金额大于原材料入库总金额！");
                    }else if(totalPrice.compareTo(zMoney)==0){
                        ruku.setJzFlag("已结清");
                    }else if(totalPrice.compareTo(zMoney)==1 && meoney.compareTo(new BigDecimal("0"))==1){
                        ruku.setJzFlag("部分结清");
                    }else {
                        ruku.setJzFlag("未结清");
                    }
                }else {
                    if(meoney.compareTo(totalPrice)==1) {
                        throw new ServiceException("支出金额大于原材料入库总金额！");
                    }else if(totalPrice.compareTo(meoney)==0){
                        ruku.setJzFlag("已结清");
                    }else if(totalPrice.compareTo(meoney)==1 && meoney.compareTo(new BigDecimal("0"))==1){
                        ruku.setJzFlag("部分结清");
                    }else {
                        ruku.setJzFlag("未结清");
                    }
                }
                ccRukuMainMapper.updateCcRukuMain(ruku);
            }
*/
        jiesuanZhikuan.setCreateTime(DateUtils.getNowDate());
        return jiesuanZhikuanMapper.insertJiesuanZhikuan(jiesuanZhikuan);
    }

    /**
     * 修改支款记账
     * 
     * @param jiesuanZhikuan 支款记账
     * @return 结果
     */
    @Override
    public int updateJiesuanZhikuan(JiesuanZhikuan jiesuanZhikuan)
    {
        jiesuanZhikuan.setUpdateTime(DateUtils.getNowDate());
        return jiesuanZhikuanMapper.updateJiesuanZhikuan(jiesuanZhikuan);
    }

    /**
     * 批量删除支款记账
     * 
     * @param ids 需要删除的支款记账主键
     * @return 结果
     */
    @Override
    public int deleteJiesuanZhikuanByIds(Long[] ids)
    {
        return jiesuanZhikuanMapper.deleteJiesuanZhikuanByIds(ids);
    }

    /**
     * 删除支款记账信息
     * 
     * @param id 支款记账主键
     * @return 结果
     */
    @Override
    public int deleteJiesuanZhikuanById(Long id)
    {
        return jiesuanZhikuanMapper.deleteJiesuanZhikuanById(id);
    }

    @Override
    public BigDecimal selectJsSurplusZhiKuan(String ywId) {
        Map<String, String> map = jiesuanZhikuanMapper.selectTjZhikuanByYwId(ywId);
        if(StringUtils.isNotEmpty(map)){
            BigDecimal zkmoney=new BigDecimal( String.valueOf(map.get("zkmoney")));
            BigDecimal yhmoney=new BigDecimal( String.valueOf(map.get("yhmoney")));
            return zkmoney.add(yhmoney);
        }else {
            return new BigDecimal("0.00");
        }
    }
}
